// newsmanager.h
#ifndef NEWSMANAGER_H
#define NEWSMANAGER_H

#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>

class NewsManager : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QString date READ date NOTIFY dateChanged)
    Q_PROPERTY(QStringList newsList READ newsList NOTIFY newsListChanged)
    Q_PROPERTY(QString weiyu READ weiyu NOTIFY weiyuChanged)
    Q_PROPERTY(QString image READ image NOTIFY imageChanged)
    Q_PROPERTY(QString headImage READ headImage NOTIFY headImageChanged)
    Q_PROPERTY(bool loading READ loading NOTIFY loadingChanged)
    Q_PROPERTY(QString errorMessage READ errorMessage NOTIFY errorMessageChanged)

public:
    explicit NewsManager(QObject *parent = nullptr);
    
    QString date() const { return m_date; }
    QStringList newsList() const { return m_newsList; }
    QString weiyu() const { return m_weiyu; }
    QString image() const { return m_image; }
    QString headImage() const { return m_headImage; }
    bool loading() const { return m_loading; }
    QString errorMessage() const { return m_errorMessage; }

public slots:
    void fetchNews();

signals:
    void dateChanged();
    void newsListChanged();
    void weiyuChanged();
    void imageChanged();
    void headImageChanged();
    void loadingChanged();
    void errorMessageChanged();

private slots:
    void onNetworkReply(QNetworkReply *reply);

private:
    void setLoading(bool loading);
    void setErrorMessage(const QString &message);
    void parseJsonResponse(const QByteArray &data);

    QNetworkAccessManager *m_networkManager;
    QString m_date;
    QStringList m_newsList;
    QString m_weiyu;
    QString m_image;
    QString m_headImage;
    bool m_loading;
    QString m_errorMessage;
};

#endif // NEWSMANAGER_H

